<template>
  <div>
    <div class="login"></div>

    <!--登录中间块-->
    <div class="login-mid">
      <div class="login-mid-top">
        <div class="shadow-top-left"></div>
        <div class="shadow-top-right"></div>
      </div>
      <div class="login-mid-mid">
        <!--捕获人脸区域-->
        <div class="videoCamera-canvasCamera">
          <video
            id="videoCamera"
            :width="videoWidth"
            :height="videoHeight"
            autoplay
          ></video>
          <canvas
            style="display: none"
            id="canvasCamera"
            :width="videoWidth"
            :height="videoHeight"
          ></canvas>

          <!--人脸特效区域-->
          <div v-if="faceImgState" class="face-special-effects-2"></div>
          <div v-else class="face-special-effects"></div>
        </div>

        <!--按钮区域-->
        <div class="face-btn">
          <button @click="faceVef()">{{ faceImgState ? '正在识别中...' : '开始识别' }}</button>
        </div>

        <!--消息区域-->
        <div class="msg">
          <div class="server-msg">{{ msg }}</div>
          <div class="welcome">Welcome to face recognition</div>
        </div>
      </div>
      <div class="login-mid-bot">
        <div class="shadow-bot-left"></div>
        <div class="shadow-bot-right"></div>
      </div>
    </div>
  </div>
</template>

<script>
import $camera from '../../camera/index.js';
export default {
  data() {
    return {
      videoWidth: 200,
      videoHeight: 200,
      msg: '',
      faceImgState: false,
      faceOption: {}
    };
  },
  mounted() {
    //调用摄像头
    this.faceOption = $camera.getCamera({
      videoWidth: 200,
      videoHeight: 200,
      thisCancas: null,
      thisContext: null,
      thisVideo: null,
      canvasId: 'canvasCamera',
      videoId: 'videoCamera'
    });

    this.getCompetence();
  },
  methods: {
    // 调用后台接口
    faceVef() {
      // 开始绘制图片
      let imageBase = $camera.draw(this.faceOption);
      if (this.faceImgState) {
        return;
      }
      this.faceImgState = true;
      if (imageBase === '' || imageBase === null || imageBase === undefined) {
        this.$message.error("图片数据为空");
      } else {
        this.$axios.post("/api/user-server/user/vef", { imageBase }).then(res => {
          console.log(res);
          this.faceImgState = false;
          // 跳转到 workbench 页面
          if (res.data.code === 200) {
            // 关闭摄像头
            this.faceOption.thisVideo.srcObject.getTracks()[0].stop();
            localStorage.setItem('token', res.data.data.accountName);
            localStorage.setItem("accountName", res.data.data.accountName);
            localStorage.setItem("userId", res.data.data.id);
            this.$message.success(res.data.msg);
            this.$router.push("/workbench"); // 修改为跳转到 workbench
          }
          if (res.data.code === 3004) {
            this.$message.error('数据库中没有人脸数据，请先录入人脸', 'warning');
          }
          if(res.data.code === 3003){
            this.$message.error('活体检测失效，请确保面部在摄像头范围内', 'warning');
          }
          if(res.data.code === 500){
            this.$message.error('人脸识别失败，请重试', 'error');
          }
        }, onerror => {
          this.faceImgState = false;
        });
      }
    }
  },
};
</script>

<style>
@import "./index.css";
</style>